import { Component, OnInit } from '@angular/core';
import { Router, ActivatedRoute } from '@angular/router';
import { ProductCate } from '../../../model/proudctCate';
import { ProductCategoryApiService } from 'src/app/api/product-category-api.service';
import { HttpParams } from '@angular/common/http';
import { NzModalService } from 'ng-zorro-antd/modal';
import { NzMessageService } from 'ng-zorro-antd/message';


@Component({
  selector: 'app-product-cate',
  templateUrl: './product-cate.component.html',
  styleUrls: ['./product-cate.component.less', '../../../styles/container.css']
})
export class ProductCateComponent implements OnInit {

  pageIndex = 1;
  pageSize = 5;
  total: number;
  productCateList: ProductCate[];
  parentId: number;
  tabelLoding = false;
  navStatusLoading = new Map<number, boolean>();
  showStatusLoading = new Map<number, boolean>()


  constructor(
    private router: Router,
    private route: ActivatedRoute,
    private productCateApi: ProductCategoryApiService,
    private modal: NzModalService,
    private message: NzMessageService,
  ) { }

  ngOnInit(): void {
    this.route.queryParams.subscribe(
      params => {
        this.getProuductCateFromServer(params.parentId ? params.parentId : 0)
      }
    )

  }

  handleAdd() {
    this.router.navigateByUrl('pms/addProductCate');
  }

  //TODO: 转移商品功能实现
  handleMoveCate() {

  }

  handleUpdate(cateId: number) {
    const url = this.router.parseUrl('pms/updateProductCate');
    url.queryParams = { id: cateId };
    this.router.navigateByUrl(url)
  }

  handleNavStatusClick(id: number) {
    this.navStatusLoading.set(id, true);
    let data = new FormData();
    const currentStatu = this.productCateList.find(cate => cate.id == id).navStatus;
    const modifyStatu = currentStatu ? 0 : 1
    data.append('ids', id.toString());
    data.append('navStatus', modifyStatu.toString());
    this.productCateApi.updateNavStatus(data).subscribe(
      response => {
        this.message.success('修改成功');
        this.productCateList.find(cate => cate.id == id).navStatus = modifyStatu;
        this.navStatusLoading.set(id, false);
      },
      err => {
        this.message.error('修改错误');
        this.navStatusLoading.set(id, false);
        console.error(err);
      }
    )
  }

  handleShowStatusClick(id: number) {
    this.showStatusLoading.set(id, true);
    let data = new FormData();
    const currentStatu = this.productCateList.find(cate => cate.id == id).showStatus;
    const modifyStatu = currentStatu ? 0 : 1
    data.append('ids', id.toString());
    data.append('showStatus', modifyStatu.toString());
    this.productCateApi.updataShowStatus(data).subscribe(
      response => {
        this.message.success('修改成功');
        this.productCateList.find(brand => brand.id == id).showStatus = modifyStatu;
        this.showStatusLoading.set(id, false);
      },
      err => {
        this.message.error('修改错误');
        this.showStatusLoading.set(id, false);
        console.error(err);
      }
    )
  }

  handleDelete(cataId: number) {
    this.modal.confirm({
      nzTitle: '确定删除此商品？',
      nzOkText: '确定',
      nzOkType: 'primary',
      nzOkDanger: true,
      nzOnOk: () => {
        this.productCateApi.delete([cataId]).subscribe(
          respon => {
            this.message.success('删除成功');
            this.getProuductCateFromServer(this.parentId);
          },
          err => {
            this.message.error('删除错误')
          }
        )
      },
      nzCancelText: '取消',
      nzMaskClosable: true,
    })
  }

  handlePageIndexChange() {
    this.getProuductCateFromServer(this.parentId);
  }

  handlePageSizeChange() {
    this.getProuductCateFromServer(this.parentId);
  }

  getProuductCateFromServer(parentId: number) {
    this.parentId = parentId;
    this.tabelLoding = true;
    let quereParam = new HttpParams(
      {
        fromObject: {
          'pageNum': this.pageIndex.toString(),
          'pageSize': this.pageSize.toString()
        }
      }
    );
    this.productCateApi.fetchListInParent(parentId, quereParam).subscribe(
      item => {
        this.tabelLoding = false;
        this.total = item.total;
        this.productCateList = item.list;
      },
      err => {
        this.tabelLoding = false;
        console.log(err);
      }
    )
  }

  handleNavDown(id: number) {
    let url = this.router.parseUrl('pms/productCate');
    url.queryParams = { 'parentId': id };
    this.router.navigateByUrl(url);
  }

}
